<?php
require_once './admin-header.php';
?>


<?php
if (isset($_SESSION['admin'])) {

	require_once './phpFiles/db/arena-park-database.php';

	$db = new ArenaParkDatabase();

	if (isset($_SESSION['text'])) {

		$users = $db->findUser($_SESSION['text'], $_SESSION['field']);
		if(!$users)
			$noUser = true;
		$noPaginar = true;
		unset($_SESSION['text']);
		unset($_SESSION['field']);
	} else if (isset($_SESSION['paginar'])) {

		$users = $db->getUserPaginator($_SESSION['from'], $_SESSION['to']);
	} else {
		$_SESSION['from'] = 0;
		$_SESSION['to'] = 10;
		$_SESSION['paginar'] = 20;
		$users = $db->getUserPaginator();
	}
//	echo $_SESSION['from'] . $_SESSION['to'] . $_SESSION['paginar'];
}
?>

<div id="container" style="font-size: 1.2em">
	<ul class="menu">
		<li id="usuariosp" >Usuarios</li>
		<li id="agenciasp" class="active">Agencias</li>
		<!--		<li id="reservasp">Reservas</li>-->
	</ul>
	<span class="clear"></span>
	<div id="usuariosContent" class="content usuariosp" style="font-size: 0.9em">
		<div id="admin-find-user" style="margin: 20px">

			<input type="text" size="25" id="findUserText" style="margin-right: 10px" />
			<strong>Por:</strong>
			<input name="userFindField" value="name" type="radio" checked="checked">Nombre
			<input name="userFindField" value="email" type="radio">Mail
			<input name="userFindField" value="phone_movil" type="radio">Movil
			<input name="userFindField" value="country" type="radio">País
			<input name="userFindField" value="postal_code" type="radio">Código Postal
			<input type="button" value="Buscar" onclick="
					findUser();
				   ">

		</div>
		<?php
		$cont = 1;
		if (isset($users) && count($users) && !isset($noUser)){
			?>		
			<div style="width: 34px; position: relative; display: inline-block; margin-left: 9px; font-weight: bold;">Sexo</div>
			<div style="width: 100px; position: relative; display: inline-block; font-weight: bold;">Nombre</div>
			<div style="width: 100px; position: relative; display: inline-block; font-weight: bold;">Apellido</div>
			<div style="width: 120px; position: relative; display: inline-block; font-weight: bold;">Dirección</div>
			<div style="width: 80px; position: relative; display: inline-block; font-weight: bold;">País</div>
			<div style="width: 90px; position: relative; display: inline-block; font-weight: bold;">Movil</div>
			<div style="width: 160px; position: relative; display: inline-block; font-weight: bold;">Mail</div>

			<?php
			$color = "";
			$coloraux = 1;
			foreach ($users as $value):
				$color = ($coloraux % 2) ? '#ccc' : '#fbd850';

				$coloraux++;
				?>
				<div id="<?php echo $value['userid']; ?>" style="display: block; padding: 0px 10px 0px 10px; background:  <?php echo $color; ?>">

					<div style="width: 34px; position: relative; display: inline-block"><?php echo $value['sex'] ?></div>
					<div style="width: 100px; position: relative; display: inline-block"><?php echo $value['name'] ?></div>
					<div style="width: 100px; position: relative; display: inline-block"><?php echo $value['last_name'] ?></div>
					<div style="width: 120px; position: relative; display: inline-block">
						<?php
						if (strlen($value['address']) > 15) {
							echo substr($value['address'], 0, 15) . '...';
						}
						else
							echo $value['address']
							?></div>
					<div style="width: 80px; position: relative; display: inline-block"><?php echo $value['country'] ?></div>
					<div style="width: 90px; position: relative; display: inline-block"><?php echo $value['phone_movil'] ?></div>
					<div style="width: 200px; position: relative; display: inline-block;"><?php echo $value['email'] ?></div>
					<div style="width: 40px; position: relative; display: inline-block; cursor: pointer; color: red; font-weight: bold" onclick="
							$('#div-user-register-admin').insertAfter($(this).parent());
							setUserForm($(this).parent().attr('id'));
							$('#userid').val($(this).parent().attr('id'));
							$('#div-user-register-admin').css('display', '');
						 ">Editar</div>
					<div style="width: 40px; position: relative; display: inline-block; cursor: pointer; color: red; font-weight: bold" onclick="
						 deleteUser($(this).parent().attr('id'));
						 ">Eliminar</div>

				</div>
				<?php
			endforeach;
			?>
			<?php if (!isset($noPaginar)): ?>
				<div style="width: 100%; margin-top: 10px" align="center">
					<form id="paginador" method="POST">
						<select name="paginar" id="paginar" style="margin-left: -53px" onchange="
							if (parseFloat(this.value))
								setPaginar(this.value);
							else {
								setPaginar('todos');
							}
								">
							<option value="10">10</option>
							<option value="0">todos</option>						
							<option value="20">20</option>
							<option value="30">30</option>
							<option value="40">40</option>
							<option value="50">50</option>
							<option value="100">50</option>
						</select>
						<script type="text/javascript">
					var paginar = <?php echo $_SESSION['paginar']; ?>;

					if (paginar < 50)
						$('#paginar').val(paginar);
					else {
						$('#paginar').val('0');
					}
						</script>

						<input name="paginadorD" id="paginadorD" type="hidden" value="" />
						<div style="position: relative; display: inline-block; cursor: pointer; margin: 5px; font-size: 1.4em; font-weight: bold; color: red" onclick="
							paginar('start');
							 "><<</div>
						<div style="position: relative; display: inline-block; cursor: pointer; margin: 5px; font-size: 1.4em; font-weight: bold; color: red" onclick="
							paginar('prev');
							 "><</div>
						<div style="position: relative; display: inline-block; cursor: pointer; margin: 5px; font-size: 1.4em; font-weight: bold; color: red" onclick="
							paginar('next');
							 ">></div>
						<div style="position: relative; display: inline-block; cursor: pointer; margin: 5px; font-size: 1.4em; font-weight: bold; color: red" onclick="
							paginar('end');
							 ">>></div>

					</form>				
				</div>
				<?php
			endif;
			?>
			<?php
		}
			elseif(isset ($noUser)){		
		?>
			<label style="margin: 20px; font-weight: bold; font-size: 1.2em;">
				No se han encontrado usurios con ese criterio de busqueda.
				
			</label>
			<?php
			}
			?>
		<div id="div-user-register-admin" class="containerForm" style="display: none">

			<form id="user-register-form" class="user-register-form">
				<input name="userid" id="userid" type="hidden" value="" class="user-data-form" />
				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Titre</label>

					<select name="sex" id="sex" class="user-data-form" required>
						<option value="">Veuillez choisir...</option>
						<option value="Mr">Mr </option>
						<option value="Mme">Mme</option>
						<option value="Mlle">Mlle</option>
					</select>

				</p>


				<p>
					<label  class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Prénom</label>

					<input name="name" id="name" class="user-data-form" size="25"  type="text" required>

				</p>


				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Nom</label>				

					<input name="last_name" id="last_name" class="user-data-form" size="25"  type="text" required>

				</p>


				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Adresse (Rue et No)</label>				

					<input name="address" id="address" class="user-data-form" size="25"  type="text" required>

				</p>

				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>C.P/localité</label>				

					<input name="postal_code" id="postal_code" class="user-data-form" size="25"  type="text" required>										

				</p>
				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Pays</label>				

					<input name="country" id="country" class="user-data-form" size="25"  type="text" required>										

				</p>

				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>E-mail</label>				

					<input id="email" type="email" name="email" size="25" class="user-data-form" required/>


				</p>

				<p>
					<label class="text-user-data-form"><label style="color: #b81900; font-size: 15px"></label>Téléphone mobile</label>				

					<input name="phone_movil" id="phone_movil" class="user-data-form" size="25"  type="text" required>


				</p>

				<p>
					<label class="text-user-data-form" style="margin-left: 67px; width: 241px">Téléphone</label>				

					<input name="phone_home" id="phone_home" class="user-data-form" size="25"  type="text">


				</p>

				<p>
					<label class="text-user-data-form" style="margin-left: 67px; width: 241px">Fax</label>				

					<input name="fax" id="fax" class="user-data-form" size="25"  type="text">

				</p>


				<p>
					<label class="text-user-data-form" style="margin-left: 67px; width: 241px">Société</label>				

					<input name="member" id="member" class="user-data-form" size="25"  type="text">

				</p>
				<br/>
				<div align="center" >
					<div class="button_loguin" style="cursor: pointer; float: none" onclick="updateUser()">
						<label style="margin-left: 15px; cursor: inherit;" >
							guardar
						</label>
					</div> 
					<div class="button_loguin" style="cursor: pointer; float: none" onclick="
					cancelar()
						 ">
						<label style="margin-left: 15px; cursor: inherit;">
							cancelar
						</label>
					</div> 
				</div>

			</form>

		</div>
	</div>
	<div class="content agenciasp">
		<form id="insert-enterprise" style="position: relative; display: block">
			<fieldset style="padding: 10px">
				<legend>Agencia de Viaje</legend>
				Código<input name="code" id="codeE" class="insertE" type="text">
				Nombre<input name="name" id="nameE" class="insertE" type="text">
				Dirección<input name="address" id="addressE" class="insertE" type="text">
				Teléfono<input name="phone" id="phoneE" class="insertE" type="text">
				<br>
				<label style="margin: 20px 0px 0px">Mail</labe><input name="emailE" id="emailE" class="insertE" type="text" style="margin: 20px 0px 0px">
					Persona de Contacto<input name="name_contact" id="name_contact" class="insertE" type="text">
					<br>
					<input id="insertE" type="button" value="Insertar" style="display: block; margin: 10px 0px; float: right" onclick="insertEnterprise()">
					</fieldset>
					</form>

					</div>
					<!--	<div class="content reservasp">
							<h3>Deberías Visitar...</h3>
							<ul>
								<li><img src="img/bullet.png" alt="-" /> <a href="http://www.plusmusica.com">www.plusmusica.com</a> - Online jukebox!</li>
								<li><img src="img/bullet.png" alt="-" /> <a href="http://www.cokidoo.com">www.cokidoo.com</a> - Nuestra startup! :)</li>
								<li><img src="img/bullet.png" alt="-" /> <a href="http://www.cokidoo.com">www.pixelartgames.com</a> - Te gustan los juegos pixel art? ;)</li>
								<li><img src="img/bullet.png" alt="-" /> <a href="http://www.dmsconsulting.es">www.dmsconsulting.es</a> - Anterior empresa</li>
							</ul>
						</div>-->
					</div>

					<script src="js/tabs.js"></script>
					<script src="js/admin.js"></script>
					</div>
					</div>

